package cn.tedu.sp02.conreoller;


import cn.tedu.sa01.pojo.Item;
import cn.tedu.sa01.server.ItemServer;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {

    @Autowired
    private ItemServer itemServer;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemServer.getItem(orderId);
        JsonResult r = JsonResult.ok().data(items);
        if (Math.random()>0.9) {
            int t = new Random().nextInt(5000);
            log.info("随机延迟 t="+t);
            Thread.sleep(t);
        }
        return r;
    }

    @PostMapping("/decreaseNumber")
    public  JsonResult<?>decreaseNumber(@RequestBody List<Item> items){
        itemServer.decreaseNumber(items);
        return  JsonResult.ok().msg("减少商品成功");
    }
}
